package com.template.app.utils;

import java.util.*;


public class CommonUtil {
	
	public static final boolean isEmpty(String str) {
        return (str == null) || "".equals(str.trim()) || "null".equals(str.trim());
    }
	
	public static final boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static final boolean isEmpty(Object obj) {
        return (obj == null) || "".equals(obj.toString().trim()) || "null".equals(obj.toString().trim());
    }

    public static final boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }
	
	public static final boolean isEmpty(String[] str) {
        return (str == null) || (str.length == 0);
    }
	
	public static final boolean isNotEmpty(String[] str) {
        return !isEmpty(str);
    }
	
	public static final boolean isEmpty(List<?> list) {
        return (list == null) || (list.size() == 0);
    }
	
	public static final boolean isNotEmpty(List<?> list) {
        return !isEmpty(list);
    }
	
	public static final String formatStringArray2String(String[] stringArray,String separator){
		StringBuffer result = null;
		if(CommonUtil.isNotEmpty(stringArray) && CommonUtil.isNotEmpty(separator)){
			result = new StringBuffer();
			for(String str : stringArray){
				result.append(separator + str);
			}
		}
		return result.toString();
	}
	
	public static final boolean isContainStr(String srcText, String[] strArray){
		if(isEmpty(strArray)){
			return false;
		}
		boolean result = true;
		if(isNotEmpty(strArray)){
			for(String str : strArray){
				if(srcText.indexOf(str)==-1){
					result = false;
					break;
				}
			}
		}
		return result;
	}
	
	 /**
     * 解析时间列表 按天来区分开来
     * @param hours
     * @return
     */
    public static Map<String, Set<String>> parseDayList(List<String> hours){
    	Map<String, Set<String>> result = new HashMap<String, Set<String>>();
    	for(String hour : hours){
    		String day = getDay(hour);
    		Set<String> dayHours;
    		if(result.containsKey(day)){
    			dayHours = result.get(day);
    		}else{
    			dayHours = new HashSet<String>();
    		}
    		dayHours.add(hour);
    		result.put(day, dayHours);
    	}
    	return result;
    }
    
    /**
     * 载取时间字符串 取前8位日期 2014120304--->20141203
     * @param beginTime
     * @return
     */
    public static String getDay(String beginTime){
        return beginTime.substring(0,8);
    }
}
